<style>
    .layui-table-click {
        background: #fff3e0;
    }

    #LAY-sys-dict-table + .layui-table-view .layui-table-body tbody > tr.layui-table-click td:last-child > div:before {
        position: absolute;
        right: 6px;
        content: "\e602";
        font-size: 12px;
        font-style: normal;
        font-family: layui-icon !important;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    #LAY-sys-dict-table + .layui-table-view .layui-table-tool-temp {
        padding-right: 0;
    }
</style>
<title>字典管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>系统管理</cite></a>
        <a><cite>字典管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md4">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto"
                     lay-filter="layadmin-dict-value-formlist">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="name" placeholder="字典名称" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn " lay-submit
                                    lay-filter="LAY-sys-dict-search">
                                搜索
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <table id="LAY-sys-dict-table" lay-filter="LAY-sys-dict-table"
                           lay-data="{id: 'dictTableId'}"></table>
                    <script type="text/html" id="toolbar">
                        <div class="layui-table-tool-temp">
                            <button class="layui-btn layui-btn-sm icon-btn" lay-event="addDict">
                                <i class="layui-icon layui-icon-add-1"></i>添加
                            </button>
                            <button class="layui-btn layui-btn-sm icon-btn layui-btn-warm" lay-event="editDict">
                                <i class="layui-icon layui-icon-edit"></i>编辑
                            </button>
                            <button class="layui-btn layui-btn-sm icon-btn layui-btn-danger" lay-event="delDict">
                                <i class="layui-icon layui-icon-delete"></i>删除
                            </button>
                        </div>
                    </script>
                </div>
            </div>
        </div>
        <div class="layui-col-md8">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto"
                     lay-filter="layadmin-dict-value-formlist">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="value" placeholder="字典值" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn" id="LAY-sys-dict-value-search" lay-submit
                                    lay-filter="LAY-sys-dict-value-search">
                                搜索
                            </button>
                            <button class="layui-btn layuiadmin-btn-dict-value" id="layuiadmin-btn-dict-value"
                                    data-type="add">
                                添加
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <table id="LAY-sys-dict-value-table" lay-filter="LAY-sys-dict-value-table"
                           lay-data="{id: 'dictValueTableId'}"></table>
                    <script type="text/html" id="table-dict-value-oper">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="editDictValue">
                            <i class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delDictValue">
                            <i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="modelDict">
    <form id="modelDictForm" lay-filter="modelDictForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">字典名称</label>
            <div class="layui-input-block">
                <input name="id" type="hidden">
                <input name="name" placeholder="请输入字典名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字典编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入字典编码" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字典描述</label>
            <div class="layui-input-block">
                <input name="desc" placeholder="请输入字典描述" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序号</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序号" type="number" class="layui-input" min="0" max="1000"
                       lay-verType="tips" lay-verify="number"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-fluid" lay-filter="modelDictFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="modelDictValue">
    <form id="modelDictValueForm" lay-filter="modelDictValueForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">字典值</label>
            <div class="layui-input-block">
                <input name="id" type="hidden">
                <input name="dictId" type="hidden">
                <input name="value" placeholder="请输入字典值" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">值编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入值编码" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序号</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序号" type="number" class="layui-input" min="0" max="1000"
                       lay-verType="tips" lay-verify="number"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-fluid" lay-filter="modelDictValueFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script>
    layui.use(['admin', 'table', 'setter'], function () {
        var $ = layui.$
            , admin = layui.admin
            , table = layui.table
            , form = layui.form
            , setter = layui.setter;

        var apiUrl = setter.apiUrl;

        var curDictId = '';
        var curDictName = '';


        //管理员管理
        table.render({
            elem: '#LAY-sys-dict-table'
            , url: apiUrl + '/mapi/dict/list'
            , toolbar: '#toolbar'
            , defaultToolbar: []
            , method: 'post'
            , page: {
                layout: ['count', 'prev', 'next', 'skip']
                , groups: 1
                , first: false
                , last: false
            }
            , contentType: 'application/json'
            , headers: {'token': layui.data(setter.tableName).token}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                {type: 'radio'}
                , {field: 'id', width: 60, title: 'id'}
                , {field: 'name', title: '字典名称'}
                , {field: 'code', title: '字典编码'}
            ]]
            , text: {
                none: '未检索到数据'
            }
        });

        //管理
        table.render({
            elem: '#LAY-sys-dict-value-table'
            , url: apiUrl + '/mapi/dict/value/list'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , headers: {'token': layui.data(setter.tableName).token}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                {field: 'id', width: 60, title: 'id'}
                , {field: 'value', title: '字典值'}
                , {field: 'code', title: '值编码'}
                , {field: 'sort', title: '排序'}
                , {title: '操作', fixed: 'right', toolbar: '#table-dict-value-oper'}
            ]]
            , text: {
                none: '未检索到数据'
            }
        });


        //监听搜索
        form.on('submit(LAY-sys-dict-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-sys-dict-table', {
                where: field
            });
        });
        form.on('submit(LAY-sys-dict-value-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-sys-dict-value-table', {
                where: field
            });
        });

        table.on('row(LAY-sys-dict-table)', function (obj) {
            var data = obj.data;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            obj.tr.find('i[class="layui-anim layui-icon"]').trigger("click");
            curDictId = data.id;
            curDictName = data.name;
            $('#LAY-sys-dict-value-search').html('搜索【' + curDictName + '】');
            $('#layuiadmin-btn-dict-value').html('添加字典【' + curDictName + '】值');

            //获取字典值
            var where = {'dictId': data.id, 'value': ''};
            //执行重载
            table.reload('LAY-sys-dict-value-table', {
                where: where
            });
        });

        table.on('tool(LAY-sys-dict-value-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'editDictValue') {
                showDictValueModel('editDictValueModel', data);
            } else if (layEvent === 'delDictValue') {
                top.layer.confirm('确定要删除' + data.value + '吗？', {
                        skin: 'layui-layer-admin'
                    }, function (index) {
                        top.layer.close(index);
                        admin.req({
                            url: apiUrl + '/mapi/dict/value/delete/' + data.id
                            , method: 'delete'
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('删除成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layui.table.reload('LAY-sys-dict-value-table');
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                    }
                );
            }
        });

        // 工具条点击事件
        table.on('toolbar(LAY-sys-dict-table)', function (obj) {
            var checkStatus = table.checkStatus('LAY-sys-dict-table');
            var hasData = checkStatus.data.length > 0;
            var layEvent = obj.event;
            if (layEvent === 'addDict') {
                showDictModel('addDictModel', {});
            } else if (layEvent === 'editDict') {
                if (!hasData) {
                    layer.msg("请选中字典数据", {
                        offset: '15px', icon: 2
                    });
                    return;
                }
                showDictModel('editDictModel', checkStatus.data[0]);
            } else if (layEvent === 'delDict') {
                if (!hasData) {
                    layer.msg("请选中字典数据", {
                        offset: '15px', icon: 2
                    });
                    return;
                }
                var data = checkStatus.data[0];
                top.layer.confirm('确定要删除字典【' + data.name + '】吗？', {
                        skin: 'layui-layer-admin'
                    }, function (index) {
                        top.layer.close(index);
                        admin.req({
                            url: apiUrl + '/mapi/dict/delete/' + data.id
                            , method: 'delete'
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('删除成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layui.table.reload('LAY-sys-dict-table');
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                    }
                );
            }
        });

        function showDictModel(type, curData) {
            var name = type == 'addDictModel' ? '添加字典' : '编辑字典';
            var method = type == 'addDictModel' ? 'post' : 'put';
            var url = type == 'addDictModel' ? apiUrl + '/mapi/dict/add' : apiUrl + '/mapi/dict/update/' + curData.id;
            admin.popup({
                title: name
                , id: 'LAY-popup-dict'
                , content: $('#modelDict').html()
                , success: function (layero, index) {
                    form.val('modelDictForm', curData);
                    form.on('submit(modelDictFormSubmit)', function (data) {
                        admin.req({
                            url: url
                            , method: method
                            , data: JSON.stringify(data.field)
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('保存成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layer.close(index);
                                        layui.table.reload('LAY-sys-dict-table');
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }

        function showDictValueModel(type, curData) {
            if (type == 'addDictValueModel' && curDictId == '') {
                layer.msg('请选中需要添加值的字典', {
                    offset: '15px', icon: 2
                });
                return;
            }
            var name = type == 'addDictValueModel' ? '【' + curDictName + '】添加值' : '编辑字典值';
            var method = type == 'addDictValueModel' ? 'post' : 'put';
            var url = type == 'addDictValueModel' ? apiUrl + '/mapi/dict/value/add' : apiUrl + '/mapi/dict/value/update/' + curData.id;
            curData['dictId'] = curDictId;
            admin.popup({
                title: name
                , id: 'LAY-popup-dict-value'
                , content: $('#modelDictValue').html()
                , success: function (layero, index) {
                    //回显数据
                    form.val('modelDictValueForm', curData);
                    // 表单提交事件
                    form.on('submit(modelDictValueFormSubmit)', function (data) {
                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                        admin.req({
                            url: url
                            , method: method
                            , data: JSON.stringify(data.field)
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('保存成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layer.close(index); //执行关闭
                                        layui.table.reload('LAY-sys-dict-value-table'); //重载表格
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        }


        //事件
        var active = {
            add: function () {
                showDictValueModel("addDictValueModel", {});
            }
        }

        $('.layui-btn.layuiadmin-btn-dict-value').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>